package com.example.mallorder.exception;

import com.alibaba.csp.sentinel.slots.block.flow.FlowException;
import com.baomidou.mybatisplus.extension.api.R;
import com.example.mallcommon.RestResult;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author yangtao
 * @descript 不过发现热点规则没法给出具体异常提示，甚至都没走到这个异常类的逻辑中，个人建议，直接使用全局异常处理即可
 */
@RestControllerAdvice
public class Oauth2ExceptionHandler {
  @ExceptionHandler(Exception.class)
  public RestResult test(Exception e) {
    String msg = "捕捉到异常";
    if (e instanceof FlowException) {
      msg = "流控了";
    }
    System.out.println(e);
    return RestResult.failed(500, msg);
  }

  /**
   * 业务异常
   *
   * @param e
   * @return
   */
  @ExceptionHandler(BusinessException.class)
  @ResponseBody
  public R handlerBusinessException(BusinessException e) {
    return R.failed("BizCodeEnume.BUSSINESS_EXCEPTION," + e.getMessage());
  }

  /**
   * 用户名和密码错误
   *
   * @param e
   * @return
   */
  @ResponseBody
  @ExceptionHandler(InvalidGrantException.class)
  public R handleInvalidGrantException(InvalidGrantException e) {
    return R.failed("BizCodeEnume.BUSSINESS_EXCEPTION," + e.getMessage());
  }

  /**
   * 账户异常(禁用、锁定、过期)
   *
   * @param e
   * @return
   */
  @ResponseBody
  @ExceptionHandler({InternalAuthenticationServiceException.class})
  public R handleInternalAuthenticationServiceException(InternalAuthenticationServiceException e) {
    return R.failed("BizCodeEnume.BUSSINESS_EXCEPTION," + e.getMessage());
  }

  @ResponseBody
  @ExceptionHandler(value = OAuth2Exception.class)
  public R handleOauth2(OAuth2Exception e) {
    return R.failed(e.getMessage());
  }
}
